package sdk

import (
	"encoding/json"
	"errors"
	"net/url"
	"strconv"

	"gitee.com/wz-open-platform/wzopen-sdk-go/constants"
	"gitee.com/wz-open-platform/wzopen-sdk-go/model"
	"gitee.com/wz-open-platform/wzopen-sdk-go/tools"
	"github.com/tidwall/gjson"
)

// 在线日志
func (c *client) OnlineLog(req *model.OnlineLogReq) (resp *model.OnlineLogResp, err error) {
	method := "GET"
	getparams := url.Values{}
	getparams.Add("device_num", req.DeviceNum)
	getparams.Add("page", strconv.Itoa(req.PageModel.Page))
	getparams.Add("limit", strconv.Itoa(req.PageModel.Limit))
	getparams = c.buildGetParam(getparams)
	bytes, err := tools.DoHttp(c.baseURL+"/open/device/log/online_log", method, map[string]string{}, nil, getparams)
	if err != nil {
		return
	}
	if gjson.GetBytes(bytes, "code").Int() != constants.SucceedCode {
		err = errors.New(gjson.GetBytes(bytes, "message").String())
		return
	}
	err = json.Unmarshal(bytes, &resp)
	return
}
